<template>
	<a-modal
		:title="title"
		:width="modalWidth"
		:bodyStyle="bodyStyle"
		:visible="visible"
		:confirmLoading="confirmLoading"
		@ok="handleOk"
		@cancel="handleCancel"
		:destroyOnClose="true"
		:maskClosable="false"
	>
		<a-spin :spinning="confirmLoading">
			<a-form :form="form" autocomplete="off">
				<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="ID" hidden>
					<a-input-number class="w100" placeholder="ID" v-decorator="['id']" />
				</a-form-item>
				<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="区域">
					<a-select
						@change="changeArea"
						placeholder="区域"
						v-decorator="[
							'areaCode',
							{
								rules: [{ required: true, message: '区域' }]
							}
						]"
					>
						<a-select-option :value="item.code" v-for="(item, index) in areaList" :key="index">{{ item.name }}</a-select-option>
					</a-select>
				</a-form-item>
				<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="学校">
					<a-select
						placeholder="学校"
						@change="changeSchool"
						v-decorator="[
							'schoolCode',
							{
								rules: [{ required: true, message: '学校' }]
							}
						]"
					>
						<a-select-option :value="item.schoolCode" v-for="(item, index) in schoolList" :key="index">{{ item.schoolName }}</a-select-option>
					</a-select>
				</a-form-item>
				<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="专业">
					<a-select
						placeholder="专业"
						v-decorator="[
							'majorCode',
							{
								rules: [{ required: true, message: '专业' }]
							}
						]"
					>
						<a-select-option :value="item.majorCode" v-for="(item, index) in majorList" :key="index">{{ item.majorName }}</a-select-option>
					</a-select>
				</a-form-item>
				<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="年份">
					<a-select
						placeholder="年份"
						v-decorator="[
							'years',
							{
								rules: [{ required: true, message: 'years' }]
							}
						]"
					>
						<a-select-option :value="item" v-for="(item, index) in yearsList" :key="index">{{ item }}</a-select-option>
					</a-select>
				</a-form-item>
				<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="正常批分数线">
					<a-input
						placeholder="正常批分数线"
						v-decorator="[
							'retrialNormalLine',
							{
								rules: [{ required: true, whitespace: true, message: '正常批分数线' }],
								getValueFromEvent: removeSpaces,
								trigger: 'blur'
							}
						]"
					/>
				</a-form-item>
				<a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="提前批分数线">
					<a-input
						placeholder="提前批分数线"
						v-decorator="[
							'retrialAdvanceLine',
							{
								rules: [{ required: true, whitespace: true, message: '提前批分数线' }],
								getValueFromEvent: removeSpaces,
								trigger: 'blur'
							}
						]"
					/>
				</a-form-item>
			</a-form>
		</a-spin>
	</a-modal>
</template>

<script>
import { api } from '@/api/index.js';
import { ModalMixins } from '@/mixins';
export default {
	name: 'addAccountModal',
	mixins: [ModalMixins],
	data() {
		return {
			areaList: [],
			schoolList: [],
			majorList: [],
			yearsList: []
		};
	},
	created() {
		
	},
	methods: {
		add(record) {
			this.visible = true;
			if (this.areaList.length == 0) {
				this.getAreasOrMajor("area");
			}
			var now = new Date().getFullYear();
			console.log(now)
			var years = [now-3,now-2,now-1,now]
			this.yearsList = years
			if (record) {
				this.title = '编辑复试线';
				this.isAdd = false;
				this.mdl = { ...record };

				//编辑时，需要回显学校，专业
				this.getSchool(record.areaCode);
				this.getMajorsBySchool(record.schoolCode);
				

				this.init();
			} else {
				this.title = '新增复试线';
				this.isAdd = true;
			}
			
		},
		init() {
			
			this.$nextTick(() => {
				this.form.setFieldsValue(this.$pick(this.mdl, 'id','areaCode','schoolCode','majorCode','years', 'retrialAdvanceLine','retrialNormalLine'));
			});
		},
		close() {
			this.$emit('close');
			this.visible = false;
		},
		handleOk() {
			const _this = this;
			// 触发表单验证
			this.form.validateFields((err, values) => {
				// 验证表单没错误
				if (!err) {
					let parameter = { ...values };
					/* 判断一下密码 */
					if (parameter.password != parameter.confirmPassWord) {
						_this.$message.error('两次输入的密码不匹配');
						return false;
					}
					_this.confirmLoading = true;
					this.$postAction(api.saveOrUpdateRetrial, Object.assign({}, parameter, !this.isAdd ? { uuid: this.mdl.uuid } : null))
						.then(res => {
							if (res.code == 200) {
								_this.$message.success('保存成功');
								_this.$emit('ok');
								_this.form.resetFields();
								_this.visible = false;
							}
						})
						.finally(() => {
							_this.confirmLoading = false;
						});
				}
			});
		},
		handleCancel() {
			this.close();
		},
		getAreasOrMajor(optiontype) {
			this.$getAction(this.$api.getAreasOrMajor, {
				optiontype
			}).then(da => {
				let list = [...da.data];
				if (optiontype == 'area') {
					this.areaList = list;
				} else {
					this.majorList = list;
				}
			});
		},
		getSchool(areaKey) {
			this.$getAction(this.$api.getSchool, {
				areaKey: areaKey
			}).then(da => {
				let list = [...da.data];
				this.schoolList = list;
			});
		},
		getMajorsBySchool(schoolCode) {
			this.$getAction(this.$api.getMajorsBySchool, {
				schoolCode
			}).then(da => {
				let list = [...da.data];
				// if (da.code == 200 && da.data && da.data.length > 0) {
					// list = da.data.map(item => {
					// 	return {
					// 		value: item.majorCode,
					// 		text: item.majorName
					// 	};
					// });
				// }
				this.majorList = list;
			});
		},
		changeArea(e) {
			this.getSchool(e);
		},
		changeSchool(e) {
			this.getMajorsBySchool(e);
		}
	}
};
</script>

<style scoped></style>
